package com.qf.lw.web;

import com.github.pagehelper.PageInfo;
import com.qf.lw.code.ResponseResult;
import com.qf.lw.entity.AdminEntity;
import com.qf.lw.entity.RoleEntity;
import com.qf.lw.pojo.AdminPojo;
import com.qf.lw.pojo.AuthPojo;
import com.qf.lw.pojo.RolePojo;
import com.qf.lw.service.IAdminService;
import com.qf.lw.service.IRoleService;
import com.qf.lw.util.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

@Controller
@SessionAttributes({"admin", "auths", "authstring"})
public class AdminController {
    @Autowired
    private IAdminService adminService;
    @Autowired
    private IRoleService roleService;

    @RequestMapping("welcome")
    public String welcome() {
        return "welcome";
    }

    @RequestMapping("login")
    public String login(AdminEntity adminEntity, Model model) {
        if (null == adminEntity) {
            model.addAttribute("msgError", "系统异常");
            return "forward:login.jsp";
        }
        if (StringUtils.isEmpty(adminEntity.getAacount())) {
            model.addAttribute("msgError", "用户名不能为空");
            return "forward:login.jsp";
        }
        if (StringUtils.isEmpty(adminEntity.getApass())) {
            model.addAttribute("msgError", "密码不能为空");
            return "forward:login.jsp";
        }

        AdminPojo adminPojo = adminService.login(adminEntity);

        if (null == adminPojo) {
            model.addAttribute("msgError", "用户名或者密码错误");
            return "forward:login.jsp";
        }
        //代码运行到这里代表登陆成功；登陆成功的需要将用户的权限保存到sessionzhong
        Set<String> auths = getAuthByAdmin(adminPojo);
        //将所有的权限放到一个字符串里面
        StringBuffer sb = new StringBuffer();
        for (String s : auths) {
            sb.append(s);
        }
        model.addAttribute("auths", auths);
        model.addAttribute("authstring", sb.toString());//所有权限拼成一个字符串，传到客户端，放入session中
        model.addAttribute("admin", adminPojo);//登陆的用户信息传到客户端
        return "index"; //代表登录成功
    }

    /**
     * 得到用户的所有权限路径
     */
  /*  private Set<String> getAuthByAdmin(AdminPojo adminPojo){

        Iterator<RolePojo> iterator = adminPojo.getRoles().iterator();
        RolePojo rp;
        Iterator<AuthPojo> authIterator;
        AuthPojo auth;
        Set<String> set = new HashSet<String>();

        while(iterator.hasNext()){ //遍历用户的所有角色
            rp = iterator.next();
            authIterator = rp.getAuths().iterator();

            while(authIterator.hasNext()){ //遍历角色里面的所有权限
                auth = authIterator.next();
                set.add(auth.getAupath()); //保存权限的路径
            }
        }

        return set;
    }*/
    public Set<String> getAuthByAdmin(AdminPojo adminPojo) {

        Set<String> set = new HashSet<String>();
        for (RolePojo rp : adminPojo.getRoles()) {
            for (AuthPojo ap : rp.getAuths()) {
                set.add(ap.getAupath());
            }
        }

        return set;
    }

    @RequestMapping("adminList")
    public ModelAndView adminList(AdminEntity adminEntity) {
        ModelAndView mv = new ModelAndView("adminlist");
        List<AdminPojo> list = adminService.qureyAdminList(adminEntity);
        PageInfo<AdminPojo> pageInfo = new PageInfo<>(list);
        mv.addObject("pageInfo", pageInfo);
        mv.addObject("adminEntity", adminEntity);
        return mv;

    }

    @RequestMapping("delAdmins")
    @ResponseBody
    public ResponseResult delAdmins(String ids) {
        ResponseResult rr = new ResponseResult();
        //ids 代表所要删除的id
        if (StringUtils.isEmpty(ids)) {
            rr.setResultCode("000001");
            rr.setResultMsg("参数不能为空");
            return rr;
        }
        //如果不为空则执行删除
        boolean bl = adminService.delAdmins(ids.split(","));
        if (!bl) {
            rr.setResultCode("00002");
            rr.setResultMsg("删除失败");
        }
        return rr;
    }

    @RequestMapping("jumpAdminadd")
    public String queryRoles(ModelMap map) {
        //查询所有角色放到页面去展示
        List<RolePojo> roles = roleService.queryRoles(null);
        map.addAttribute("roles", roles);
        return "admin-add";
    }


    //添加用户
    @RequestMapping("adminAdd")
    public String adminAdd(AdminEntity adminEntity){
            adminService.adminAdd(adminEntity);
            return  "forward:adminList";
    }
    //修改用户
    @RequestMapping("jumpAdminEdit")
    @ResponseBody
    public ModelAndView queryRoles1(RoleEntity roleEntity,  @Param("aid") String aid) {
        //查询所有角色放到页面去展示
        ModelAndView mv = new ModelAndView();
        List<RolePojo> roles = roleService.queryRoles(roleEntity);
        AdminPojo adminPojo = adminService.queryAuthById(aid);
        mv.addObject("roles", roles);
        mv.addObject("adminPojo", adminPojo);
        mv.setViewName("admin-edit");
        return mv;
    }
    @RequestMapping("updateAdmin")
    public String updateAdmin(AdminEntity AdminEntity) {
        adminService.updateAdmin(AdminEntity);
        return "forward:adminList";
    }
}
